package org.epay.http.filter;

import java.util.Date;

import org.epay.action.IdentityAction;
import org.epay.action.PCErrorPack;
import org.epay.action.TokenAction;
import org.epay.data.UserData;
import org.epay.http.HOpCode;
import org.epay.model.base.Token;
import org.epay.protobuf.http.PCErrorProto.PCError;
import org.epay.protobuf.http.PCErrorProto.PCErrorCode;
import org.grain.httpserver.HttpException;
import org.grain.httpserver.HttpPacket;
import org.grain.httpserver.IHttpFilter;

public class TokenHttpFilter implements IHttpFilter {

	@Override
	public boolean httpFilter(HttpPacket httpPacket) throws HttpException {
		if (HOpCode.LOGIN.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.GET_ADMIN_TOKEN.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.VERIFY_NOTIFY.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.CREATE_ORDER_RECORD.equals(httpPacket.hSession.headParam.hOpCode)) {
			// 可以通过
			return true;
		}
		if (httpPacket.hSession.headParam.token == null) {
			PCError errorPack = PCErrorPack.create(PCErrorCode.ERROR_CODE_4, httpPacket.hSession.headParam.hOpCode);
			throw new HttpException(HOpCode.PC_ERROR, errorPack);
		}
		Token token = TokenAction.getTokenById(httpPacket.hSession.headParam.token);
		// 说明是第三方用户或者管理用户没登录
		if (token == null) {
			if (HOpCode.GET_ORDER_RECORD.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.GET_PAY_HTML.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.GET_RETURN_URL.equals(httpPacket.hSession.headParam.hOpCode)) {
				UserData userData = IdentityAction.getUser(httpPacket.hSession.headParam.token);
				if (userData == null) {
					PCError errorPack = PCErrorPack.create(PCErrorCode.ERROR_CODE_4, httpPacket.hSession.headParam.hOpCode);
					throw new HttpException(HOpCode.PC_ERROR, errorPack);
				}
				httpPacket.hSession.otherData = userData;
				return true;
			} else {
				PCError errorPack = PCErrorPack.create(PCErrorCode.ERROR_CODE_4, httpPacket.hSession.headParam.hOpCode);
				throw new HttpException(HOpCode.PC_ERROR, errorPack);
			}
		} else {
			Date date = new Date();
			// token过期
			if (date.getTime() > token.getTokenExpireTime().getTime()) {
				TokenAction.deleteToken(token.getTokenId());
				PCError errorPack = PCErrorPack.create(PCErrorCode.ERROR_CODE_4, httpPacket.hSession.headParam.hOpCode);
				throw new HttpException(HOpCode.PC_ERROR, errorPack);
			} else {
				TokenAction.updateToken(token.getTokenId());
			}
			if (HOpCode.CREATE_APP.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.UPDATE_APP.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.GET_APP.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.GET_APP_LIST.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.UPDATE_ORDER_RECORD.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.GET_ORDER_RECORD.equals(httpPacket.hSession.headParam.hOpCode) || HOpCode.GET_ORDER_RECORD_LIST.equals(httpPacket.hSession.headParam.hOpCode)) {
				httpPacket.hSession.otherData = token;
				return true;
			} else {
				PCError errorPack = PCErrorPack.create(PCErrorCode.ERROR_CODE_4, httpPacket.hSession.headParam.hOpCode);
				throw new HttpException(HOpCode.PC_ERROR, errorPack);
			}
		}
	}

}
